package com.bw.services.properties.model

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import com.bw.lib_base.base.BaseViewModel
import com.bw.services.properties.entity.PropertiesEntity
import com.bw.services.properties.entity.PropertiesEntityItem
import com.bw.services.visit.model.VisitRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class PropertiesViewModel  @Inject constructor(val propertiesRepo: PropertiesRepo):BaseViewModel(){
   private val _propertiesData = MutableStateFlow<List<PropertiesEntityItem>?>(null)
    val propertiesData:StateFlow<List<PropertiesEntityItem>?> =_propertiesData

    fun getPropertiesData() {
        viewModelScope.launch {
            if (isInitLife()){
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val properties = propertiesRepo.getProperties()
                    _propertiesData.emit(properties)
                }
            }
        }
    }
 }